﻿@model SuperDumpService.ViewModels.ReportViewModel
@using SuperDumpService.Helpers
@using SuperDumpService.Models
@using SuperDumpService.ViewModels
@inject IAuthorizationHelper AuthorizationHelper
@{
	ViewData["Title"] = "Report";
}
@if (Model == null) {
	<h2>Requested dump does not exist.</h2>
	<p>... but might get created in a few seconds...</p>

	<script>
		setTimeout(function () {
			window.location.reload(1);
		}, 5000);
	</script>

} else {
	<div class="wrapper superdump-report">
		<h2>Dump Id: @Model.Id.DumpId</h2>
		@if (!Model.IsRelationshipsPopulated) {
			<div>
				<span class="glyphicon glyphicon-exclamation-sign"></span> <strong>Similarity repository</strong> is being populated right now. Similarities might not be complete yet.
			</div>
		}
		@if (!Model.IsJiraIssuesPopulated) {
			<div>
				<span class="glyphicon glyphicon-exclamation-sign"></span> <strong>Jira issue repository</strong> is populated right now. Linked jira issues might not be complete yet.
			</div>
		}

		<!-- Page Content -->
		<div id="content">
			<div style="display: block; float: right; position: absolute; right: 15px; width: 450px; ">
				@if (Model.RetentionViewModel.IsDumpAvailable && AuthorizationHelper.CheckPolicy(User, LdapCookieAuthenticationExtension.UserPolicy)) {
					@using (Html.BeginForm("Rerun", "Home")) {
						<div class="form-group text-right">
							<input type="hidden" name="bundleId" value="@Model.Id.BundleId" />
							<input type="hidden" name="dumpId" value="@Model.Id.DumpId" />
							<input type="submit" value="Rerun Analysis" class="btn btn-danger" />
						</div>
					}
				}

				@if (Model.UseJiraIntegration) {
					<div class="card">
						<div class="card-header">Jira Issues</div>
						<div class="card-body">
							@if (!Model.MainBundleJiraIssues.Any()) {
								<h6 class="card-subtitle mb-2 text-muted">No Jira Issues Found</h6>
							} else {
								<div>
									<partial name="_JiraIssues" model="Model.MainBundleJiraIssues" />
								</div>
							}
						</div>
					</div>
					<br />
				}

				@if (Model.SimilarityDetectionEnabled) {
					<div class="card" style="z-index:999">
						<div class="card-header">Similar Dumps</div>
						<div class="card-body">
							@if (!Model.Similarities.Any()) {
								<h6 class="card-subtitle mb-2 text-muted">No similar dumps found.</h6>
							} else {
								var dupls = Model.Similarities.Where(x => x.Value >= 0.8);
								<p class="card-text">@dupls.Count() similar dumps found.</p>
								<button class="btn btn-primary" type="button" data-toggle="collapse" data-target="#collapseDuplicates" aria-expanded="false" aria-controls="collapseDuplicates">
									Hide/Show
								</button>
								<div class="collapse show" id="collapseDuplicates">
									<ul class="list-group list-group-flush">
										@foreach (var rel in dupls.OrderByDescending(x => x.Value + (Model.UseJiraIntegration && Model.SimilarDumpIssues.ContainsKey(x.Key.BundleId) ? 100 : 0))) {
											<li class="list-group-item">
												<div class="small">(@rel.Key.Created)</div>
												<a asp-controller="Similarity" asp-action="CompareDumps" asp-route-bundleId1="@Model.Id.BundleId" asp-route-dumpId1="@Model.Id.DumpId" asp-route-bundleId2="@rel.Key.Id.BundleId" asp-route-dumpId2="@rel.Key.Id.DumpId">
													<span class="font-weight-bold text-success">@Math.Round(rel.Value * 100.0, 0)&#37;</span>
												</a>
												<a href="@SuperDumpService.Helpers.Utility.GetDumpUrl(rel.Key.Id)">@System.IO.Path.GetFileName(rel.Key.DumpFileName)</a>
												@if (Model.UseJiraIntegration && Model.SimilarDumpIssues.TryGetValue(rel.Key.BundleId, out IEnumerable<JiraIssueModel> issues)) {
													<div class="mt-2 small">
														<partial class="col-sm-8" id="duplicateJiraIssues" name="_JiraIssues" model="issues" />
													</div>
												}
											</li>
										}
									</ul>
								</div>
							}
						</div>
					</div>
				}
			</div>
			<dl class="row courier-small compact">
				@if (!string.IsNullOrEmpty(Model.BundleFileName) && Model.BundleFileName != Model.DumpFileName) {
					<dt class="col-sm-2 text-right">BundleFileName:</dt>
					<dd class="col-sm-10">@Model.BundleFileName</dd>
				}
				@if (!string.IsNullOrEmpty(Model.DumpFileName)) {
					<dt class="col-sm-2 text-right">DumpFileName:</dt>
					<dd class="col-sm-10">
						@Model.DumpFileName
						@if (Model.RetentionViewModel.IsDumpAvailable && AuthorizationHelper.CheckPolicy(User, LdapCookieAuthenticationExtension.UserPolicy)) {
							<a>
								(
								<interactive-link model="@(Model)">
									Start interactive session
								</interactive-link>
								)
							</a>
						}
					</dd>
				}
				<dt class="col-sm-2 text-right">Analyzed at:</dt>
				<dd class="col-sm-10">@Model.TimeStamp.ToString("yyyy-MM-dd HH:mmzzz")</dd>

				@if (Model.UseAutomaticDumpDeletion) {
					<dt class="col-sm-2 text-right">Planned Deletion:</dt>
					<dd class="col-sm-10">
						@using (Html.BeginForm("ExtendRetentionTime", "Home", FormMethod.Post, new { @class = "form-inline" })) {
							<partial name="_RetentionTime" model="Model.RetentionViewModel" />
							@if (Model.RetentionViewModel.IsDumpAvailable) {
								<input type="hidden" name="bundleId" value="@Model.Id.BundleId" />
								<input type="hidden" name="dumpId" value="@Model.Id.DumpId" />
								<div>&nbsp;(<input type="submit" value="extend to @Model.DumpRetentionExtensionDays days" class="btn-link my-0 py-0 border-bottom-0 border-top-0" />)</div>
							}
						}
					</dd>
				}

				@foreach (var customProp in Model.CustomProperties) {
					<dt class="col-sm-2 text-right">
						@customProp.Key:
					</dt>
					<dd class="col-sm-10">
						<possible-link href="@customProp.Value" is-external="true"> @customProp.Value </possible-link>
					</dd>
				}

				<dt class="col-sm-2 text-right">Files:</dt>
				<dd class="col-sm-10">
					<div class="filegroups">
						@{ var sequence = new List<string> { "Primary Dump", "Other files", "Results", "Metadata", "Logs" }; }
						@foreach (var filetype in Model.Files.Select(x => Utility.GetEnumDescription(x.FileEntry.Type)).Distinct()
						  .OrderBy(x => sequence.IndexOf(x) < 0 ? int.MaxValue : sequence.IndexOf(x))) { // somewhat ugly hack to control the order of file categories
							<dl class="row filegroup filegroup-@filetype.ToString().Replace(" ", "_")">
								<dt class="col-sm-2 text-right filesize">@filetype.ToString()</dt>
								<dd class="col-sm-10">
									<ul class="flat">
										@foreach (SDFileInfo file in Model.Files.Where(x => Utility.GetEnumDescription(x.FileEntry.Type) == filetype).OrderBy(x => x.FileEntry.FileName)) {
											<li>
												@{Html.RenderPartial("_File", new FileViewModel(Model.Id, file));}
											</li>
										}
									</ul>
								</dd>
							</dl>
						}
					</div>
				</dd>
			</dl>

			<div class="container-fluid">
				@if (!string.IsNullOrEmpty(Model.SDResultReadError)) {
					<h2>Something went wrong when reading SDResult.</h2>
					<pre>@Model.SDResultReadError</pre>
				} else if (Model.DumpStatus == DumpStatus.Failed) {
					<h2>Analysis failed with errors, no report avaliable.</h2>
					<pre>Error: @Model.AnalysisError</pre>
				} else if (Model.DumpStatus != DumpStatus.Finished) {
					<h2>hang tight, still processing</h2>
					<script>
						setTimeout(function () {
							window.location.reload(1);
						}, 5000);
					</script>
				} else if (Model.DumpType == DumpType.Empty) {
					<p>No Dump found in Bundle</p>
				} else if (Model.Result == null) {
					@if (string.IsNullOrEmpty(Model.SDResultReadError)) {
						<p>SuperDump Model is null for unknown reason.</p>
					} else {
						<p>SuperDump Model is null, because of error:</p>
						<pre>@Model.SDResultReadError</pre>
					}

					if (Model.CustomTextResult != null) {
						<p>Falling back to CustomTextResult.</p>
						<section>
							<pre>@Model.CustomTextResult</pre>
						</section>
					}
				} else {
					<section>
						@{Html.RenderPartial("_Summary");}
					</section>
					<section>
						@{Html.RenderPartial("_System");}
					</section>
					<section>
						@{Html.RenderPartial("_Threads");}
					</section>
					<section>
						@{Html.RenderPartial("_Modules");}
					</section>
					<section>
						@{Html.RenderPartial("_Domains");}
					</section>
					<section>
						@{Html.RenderPartial("_Memory");}
					</section>
				}
			</div>
		</div>

	</div>
}
